package com.imusic;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Component
public class OrderService {
  
  
  private static final Logger log = LoggerFactory.getLogger(OrderService.class);

  @Autowired
  RestTemplate restTemplate;

  @HystrixCommand(fallbackMethod="defaultOrder")
  public String startOrder() throws InterruptedException {
    Thread.sleep(200);
    String service3 = restTemplate.getForObject("http://account-service/getAccount", String.class);
    log.info("Got response from Account Service [{}]", service3);
    String service4 = restTemplate.getForObject("http://product-service/getProduct", String.class);
    log.info("Got response from Product Service [{}]", service4);
    return String.format("Got response from Account Service [%s] and then Got response from Product Service [%s]", service3, service4);

  }
  
  public String  defaultOrder() {
    return "account-service is error!";
  }

  @Bean
  @LoadBalanced
  RestTemplate restTemplate() {
    return new RestTemplate();
  }
}
